package com.lmiot.xyclick.AD;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.lkx.liandianqinew.R;
import com.lmiot.xyclick.AD.SDK.ADSDK;
import com.lmiot.xyclick.Activity.FirstGuideActivity;
import com.lmiot.xyclick.Activity.MainActivity;
import com.lmiot.xyclick.Activity.WebViewActivity;
import com.lmiot.xyclick.App.MyApp;
import com.lmiot.xyclick.Bean.XyNoticBean;
import com.lmiot.xyclick.BuildConfig;
import com.lmiot.xyclick.Util.DataUtil;
import com.lmiot.xyclick.Util.FileUtils;
import com.lmiot.xyclick.Util.LayoutDialogUtil;
import com.lmiot.xyclick.Util.LogUtil;
import com.lmiot.xyclick.Util.SpFlag;
import com.youyi.yypermissionlibrary.SDK.OnPerListener;
import com.youyi.yypermissionlibrary.SDK.YYPerUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.callback.FileCallBack;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import okhttp3.Call;

public class SplashActivity extends Activity {
    private static final String TAG = "HWSplashActivity";
    private Dialog mDailog;
    private Dialog mDialog;
    private Intent mIntent;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        requestWindowFeature(1);
        getWindow().setFlags(1024, 1024);
        setContentView(R.layout.activity_splash);
        showTip();
    }

    private void showTip() {
        if (DataUtil.getFisrtTip(this)) {
            Dialog dialog = this.mDialog;
            if (dialog != null && dialog.isShowing()) {
                this.mDialog.dismiss();
            }
            Dialog createDailog = LayoutDialogUtil.createDailog(this, R.layout.dialog_xy_agreement);
            this.mDialog = createDailog;
            createDailog.setCancelable(false);
            final CheckBox checkBox = (CheckBox) this.mDialog.findViewById(R.id.id_checkbox);
            ((TextView) this.mDialog.findViewById(R.id.id_content)).setText("感谢您使用本软件，应国家工信部要求，同时为了更好地保护您的合法权益。请您务必在使用本软件前，认真阅读《服务协议》和《隐私政策》里面的全部条款，建议您在理解条款并接受的情况下才点击同意，一旦您点击了同意，即表示您已经阅读并接受条款的全部内容，该条款内容将构成对您具有法律约束力的法律文件。");
            ((TextView) this.mDialog.findViewById(R.id.tv_server)).setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    SplashActivity.this.mIntent = new Intent(SplashActivity.this, WebViewActivity.class);
                    SplashActivity.this.mIntent.putExtra(SpFlag.k, "《服务协议》");
                    if (ADSDK.nowVersion.startsWith("HW")) {
                        SplashActivity.this.mIntent.putExtra("url", "https://ai.xiaoyizhineng.com:441/download/HelpVideo/autoclick/serverh.html");
                    } else {
                        SplashActivity.this.mIntent.putExtra("url", "https://ai.xiaoyizhineng.com:441/download/HelpVideo/autoclick/server.html");
                    }
                    SplashActivity splashActivity = SplashActivity.this;
                    splashActivity.startActivity(splashActivity.mIntent);
                }
            });
            ((TextView) this.mDialog.findViewById(R.id.tv_private)).setOnClickListener(new View.OnClickListener() {
                /* class com.lmiot.xyclick.AD.SplashActivity.AnonymousClass2 */

                public void onClick(View view) {
                    SplashActivity.this.mIntent = new Intent(SplashActivity.this, WebViewActivity.class);
                    SplashActivity.this.mIntent.putExtra(SpFlag.k, "《隐私政策》");
                    if (ADSDK.nowVersion.startsWith("HW")) {
                        SplashActivity.this.mIntent.putExtra("url", "https://ai.xiaoyizhineng.com:441/download/HelpVideo/autoclick/privateh.html");
                    } else {
                        SplashActivity.this.mIntent.putExtra("url", "https://ai.xiaoyizhineng.com:441/download/HelpVideo/autoclick/private.html");
                    }
                    SplashActivity splashActivity = SplashActivity.this;
                    splashActivity.startActivity(splashActivity.mIntent);
                }
            });
            ((TextView) this.mDialog.findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    SplashActivity.this.mDialog.dismiss();
                    SplashActivity.this.finish();
                }
            });
            ((TextView) this.mDialog.findViewById(R.id.tv_sure)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    if (!checkBox.isChecked()) {
                        Toast.makeText(SplashActivity.this, "请先勾选同意按钮才可继续！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    DataUtil.setFisrtTip(SplashActivity.this, false);
                    SplashActivity.this.mDialog.dismiss();
                    MyApp.getInstance().startInit();
                    SplashActivity.this.first00();
                }
            });
            return;
        }
        MyApp.getInstance().startInit();
        first00();
    }

    private void first00() {
        if (!DataUtil.getHasShowGuide(this)) {
            startActivity(new Intent(this, FirstGuideActivity.class));
            finish();
            return;
        }
        DataUtil.setCheckSPlashActivity(this, true);
        if (DataUtil.getPermission(this)) {
            requestPermission();
        } else if (ADSDK.nowVersion.startsWith("HW") || ADSDK.nowVersion.startsWith(BuildConfig.FLAVOR)) {
//            if (DataUtil.getVip(MyApp.getContext())) {
//                jumMain();
//            } else {
//                showADMethod();
//            }
            jumMain();
        } else if (DataUtil.getJumpPer(MyApp.getContext())) {
            jumMain();
        } else {
            showPermissionDialog();
        }
    }

    private void showPermissionDialog() {
        Dialog createDailog = LayoutDialogUtil.createDailog(this, R.layout.dialog_request_permission);
        this.mDailog = createDailog;
        createDailog.setCancelable(false);
        ((TextView) this.mDailog.findViewById(R.id.tv_cancel)).setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                SplashActivity.this.mDailog.dismiss();
                DataUtil.setJumpPer(MyApp.getContext(), true);
                SplashActivity.this.judge("TTT");
            }
        });
        ((TextView) this.mDailog.findViewById(R.id.tv_sure)).setOnClickListener(new View.OnClickListener() {
            /* class com.lmiot.xyclick.AD.SplashActivity.AnonymousClass6 */

            public void onClick(View view) {
                SplashActivity.this.mDailog.dismiss();
                DataUtil.setJumpPer(MyApp.getContext(), false);
                SplashActivity.this.requestPermission();
            }
        });
    }

    private void requestPermission() {
        YYPerUtils.sd(new OnPerListener() {
            @Override
            public void result(boolean z, String str) {
                if (z) {
                    DataUtil.setPermission(SplashActivity.this, true);
                    SplashActivity splashActivity = SplashActivity.this;
                    if (splashActivity.isNetworkConnected(splashActivity)) {
                        SplashActivity.this.downNotic();
                    } else {
                        SplashActivity.this.jumMain();
                    }
                } else {
                    if (ADSDK.nowVersion.startsWith("HW")) {
                        DataUtil.setJumpPer(MyApp.getContext(), true);
                    }
                    SplashActivity.this.judge("TTT");
                }
            }
        });
    }

    private void downNotic() {
        File filesDir = MyApp.getContext().getFilesDir();
        LogUtil.d(TAG, "https://ai.xiaoyizhineng.com:441/download/HelpVideo/autoclick/noticText.txt");
        ((GetBuilder) OkHttpUtils.get().url("https://ai.xiaoyizhineng.com:441/download/HelpVideo/autoclick/noticText.txt")).build().execute(new FileCallBack(filesDir.getAbsolutePath(), "nt.xy") {

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(File file, int id) {
                if (file != null) {
                    try {
                        XyNoticBean xyNoticBean = (XyNoticBean) new Gson().fromJson(FileUtils.readFileToString(file), XyNoticBean.class);
                        if (xyNoticBean != null) {
                            DataUtil.nowNoicBean = xyNoticBean;
                            SplashActivity.this.judge(xyNoticBean.getUploadVersion());
                            SplashActivity.this.saveDenyList(xyNoticBean.getDenylist());
                            return;
                        }
                        SplashActivity.this.jumMain();
                    } catch (Exception e) {
                        e.printStackTrace();
                        SplashActivity.this.jumMain();
                    }
                } else {
                    SplashActivity.this.jumMain();
                }
            }
        });
    }

    private void saveDenyList(List<XyNoticBean.DenylistBean> list) {
        if (list != null && list.size() > 0) {
            StringBuffer stringBuffer = new StringBuffer();
            for (XyNoticBean.DenylistBean denylistBean : list) {
                stringBuffer.append(denylistBean.getPackName());
            }
            DataUtil.setDenyList(MyApp.getContext(), stringBuffer.toString().trim());
        }
    }

    private void jumMain() {
        LogUtil.d(TAG, "jumMain");
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    public boolean isNetworkConnected(Context context) {
        NetworkInfo activeNetworkInfo;
        if (context == null || (activeNetworkInfo = ((ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE)).getActiveNetworkInfo()) == null) {
            return false;
        }
        return activeNetworkInfo.isAvailable();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void judge(String str) {
        try {
            if (TextUtils.isEmpty(str)) {
                if (DataUtil.getVip(MyApp.getContext())) {
                    jumMain();
                } else {
                    showADMethod();
                }
            } else if (str.contains(ADSDK.nowVersion)) {
                ADSDK.mIsGDT = true;
                jumMain();
            } else {
                ADSDK.mIsGDT = false;
                if (DataUtil.getVip(MyApp.getContext())) {
                    jumMain();
                } else {
                    showADMethod();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showADMethod() {
        if (!ADSDK.nowVersion.startsWith(BuildConfig.FLAVOR) && !ADSDK.nowVersion.startsWith("HW")) {
            ADSDK.getInstance().chosADShow(this, true, new ADSDK.OnADFinishListener() {
                /* class com.lmiot.xyclick.AD.SplashActivity.AnonymousClass10 */

                @Override // com.lmiot.xyclick.AD.SDK.ADSDK.OnADFinishListener
                public void result(boolean z) {
                    SplashActivity.this.jumMain();
                }
            });
        } else if (getInstallHour(MyApp.getContext()) >= 1) {
            ADSDK.getInstance().chosADShow(this, true, new ADSDK.OnADFinishListener() {
                /* class com.lmiot.xyclick.AD.SplashActivity.AnonymousClass9 */

                @Override // com.lmiot.xyclick.AD.SDK.ADSDK.OnADFinishListener
                public void result(boolean z) {
                    SplashActivity.this.jumMain();
                }
            });
        } else {
            jumMain();
        }
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i == 4 || i == 3) {
            return true;
        }
        return super.onKeyDown(i, keyEvent);
    }

    public int getInstallHour(Context context) {
        try {
            long j = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime;
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(j));
            return (int) ((((System.currentTimeMillis() - j) / 60) / 60) / 1000);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 0;
        }
    }
}
